package com.develop.alarm.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;

import com.develop.alarm.bean.AlarmInfo;
import com.develop.common.JDBCUtil;

public class AlarmService {

	public static void main(String[] args) {
		System.out.println(getMaxAlarmSeqByProvinceAndFirmName("JS", "HUAWEI"));
//		selectFunction();
//		LinkedList<AlarmInfo> alarmList=new LinkedList<AlarmInfo>();
//		AlarmInfo alarm=new AlarmInfo(101, "告警", 1, "aa", 1, "2019-06-01", "2019-06-02", "11111", "2222", "helloworld", "124323", "1232", "24343", "34234", "342343", "343243", "2343", "34343", "3434", "34234", "343243", "34324", "342343");
//		alarmList.add(alarm);
//		batchInsertAlarmInfo(alarmList);
	}
	public static int getMaxAlarmSeqByProvinceAndFirmName(String province,String firmName){
		int maxAlarmSeq=0;
		try {
			Connection conn = JDBCUtil.getConnection();
			String sql = "SELECT max(alarmSeq)alarmSeq FROM realTimeAlarm";
			PreparedStatement st = conn.prepareStatement(sql);
			ResultSet rs = st.executeQuery();
			while (rs.next()) {
				maxAlarmSeq=Integer.parseInt(rs.getString("alarmSeq"));
//				System.out.println(rs.getString("alarmSeq"));
			}
			JDBCUtil.closeResource(conn, rs, st);
		} catch (SQLException ex) {
			System.out.println(ex.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return maxAlarmSeq;
	
	}
	/**
	 * 批量插入告警数据
	* alarmList 告警列表
	* province  省份  例如：JS（江苏）
	* firmName  厂家  例如：HUAWEI（华为）
	* @author xxj
	* @date 2019年6月20日
	 */
	public static void batchInsertAlarmInfo(LinkedList<AlarmInfo> alarmList,String province,String firmName) {
		try {
			if(alarmList.isEmpty()){
				System.out.println("没有接收到数据！");
			}else{
					System.out.println(alarmList.size());
					Connection conn = JDBCUtil.getConnection();
					conn.setAutoCommit(false);
					String sql = "insert into realtimealarm(alarmSeq,alarmTitle,alarmStatus,alarmType,origSeverity,eventTime,omcReceivedTime,alarmId,"
							+ "specificProblemID,specificProblem,omcUID,neUID,neName,neType,objectUID,objectName,objectType,locationInfo,eSerialNum,addInfo,rNeUID,rNeName,rNeType,province,firmName) "
							+ "value (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
					PreparedStatement prest = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
							ResultSet.CONCUR_READ_ONLY);
					long currenttime=System.currentTimeMillis();
					for (AlarmInfo alarm:alarmList) {
						prest.setInt(1,alarm.getAlarmSeq());
						prest.setString(2, alarm.getAlarmTitle());
						prest.setInt(3, alarm.getAlarmStatus());
						prest.setString(4, alarm.getAlarmType());
						prest.setInt(5, alarm.getOrigSeverity());
						prest.setString(6, alarm.getEventTime());
						prest.setString(7, alarm.getOmcReceivedTime());
						prest.setString(8, alarm.getAlarmId());
						prest.setString(9, alarm.getSpecificProblemID());
						prest.setString(10, alarm.getSpecificProblem());
						prest.setString(11, alarm.getOmcUID());
						prest.setString(12, alarm.getNeUID());
						prest.setString(13, alarm.getNeName());
						prest.setString(14, alarm.getNeType());
						prest.setString(15, alarm.getObjectUID());
						prest.setString(16, alarm.getObjectName());
						prest.setString(17, alarm.getObjectType());
						prest.setString(18, alarm.getLocationInfo());
						prest.setString(19, alarm.geteSerialNum());
						prest.setString(20, alarm.getAddInfo());
						prest.setString(21, alarm.getrNeUID());
						prest.setString(22, alarm.getrNeName());
						prest.setString(23, alarm.getrNeType());
						prest.setString(24, province);
						prest.setString(25, firmName);
						prest.addBatch();
					}
					long btime=System.currentTimeMillis()-currenttime;
					System.out.println("数据保存成功！插入数据条数:"+alarmList.size()+"====耗时===="+btime+"ms");
					prest.executeBatch();
					conn.commit();
					
//					conn.close();
				
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	public static void batchInsertAlarmInfo_all(LinkedList<AlarmInfo> alarmList) {
		try {
			if(alarmList.isEmpty()){
				System.out.println("没有接收到数据！");
			}else{
					System.out.println(alarmList.size());
					Connection conn = JDBCUtil.getConnection();
					conn.setAutoCommit(false);
					String sql = "insert into alarm_all(alarmSeq,alarmTitle,alarmStatus,alarmType,origSeverity,eventTime,omcReceivedTime,alarmId,"
							+ "specificProblemID,specificProblem,omcUID,neUID,neName,neType,objectUID,objectName,objectType,locationInfo,eSerialNum,addInfo,rNeUID,rNeName,rNeType) "
							+ "value (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
					PreparedStatement prest = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
							ResultSet.CONCUR_READ_ONLY);
					long currenttime=System.currentTimeMillis();
					for (AlarmInfo alarm:alarmList) {
						prest.setInt(1,alarm.getAlarmSeq());
						prest.setString(2, alarm.getAlarmTitle());
						prest.setInt(3, alarm.getAlarmStatus());
						prest.setString(4, alarm.getAlarmType());
						prest.setInt(5, alarm.getOrigSeverity());
						prest.setString(6, alarm.getEventTime());
						prest.setString(7, alarm.getOmcReceivedTime());
						prest.setString(8, alarm.getAlarmId());
						prest.setString(9, alarm.getSpecificProblemID());
						prest.setString(10, alarm.getSpecificProblem());
						prest.setString(11, alarm.getOmcUID());
						prest.setString(12, alarm.getNeUID());
						prest.setString(13, alarm.getNeName());
						prest.setString(14, alarm.getNeType());
						prest.setString(15, alarm.getObjectUID());
						prest.setString(16, alarm.getObjectName());
						prest.setString(17, alarm.getObjectType());
						prest.setString(18, alarm.getLocationInfo());
						prest.setString(19, alarm.geteSerialNum());
						prest.setString(20, alarm.getAddInfo());
						prest.setString(21, alarm.getrNeUID());
						prest.setString(22, alarm.getrNeName());
						prest.setString(23, alarm.getrNeType());
						prest.addBatch();
					}
					long btime=System.currentTimeMillis()-currenttime;
					System.out.println("数据保存成功！插入数据条数:"+alarmList.size()+"====耗时===="+btime+"ms");
					prest.executeBatch();
					conn.commit();
					
//					conn.close();
				
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	public static void selectFunction() {
		try {
			// 1.获取连接
			Connection conn = JDBCUtil.getConnection();

			// 3.sql语句
			String sql = "SELECT * FROM realTimeAlarm limit 10";

			// 3.获取SQL执行者
			PreparedStatement st = conn.prepareStatement(sql);

			// 4.设置参数

			// 5.执行sql语句
			ResultSet rs = st.executeQuery();

			// 6.处理数据
			while (rs.next()) {
				System.out.println(rs.getString("alarmSeq") + ":" + rs.getString("alarmTitle"));
			}

			// 7.释放资源
			JDBCUtil.closeResource(conn, rs, st);

		} catch (SQLException ex) {
			System.out.println(ex.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}
